package cn.szsys.integration.runner.entity;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @author Ryan
 * @since 2025-05-08
 **/
@Data
@Slf4j
public class AuthorizationConfig implements Serializable {
    private List<Accessor> accessors;
    private Map<String, Accessor> akMap;

    public void initAkMap() {
        akMap = accessors.stream().collect(Collectors.toMap(Accessor::getAk, s -> s, (old, n) -> {
            log.error("授权文件存在重复的AK：{}", old.getAk());
            return n;
        }, ConcurrentHashMap::new));
    }

    /**
     * 根据AK获取Accessor
     * @author Ryan
     * @param ak ak
     * @return {@link Accessor}
     */
    public Accessor getAccessorByAk(String ak) {
        return akMap.get(ak);
    }
}
